// mine/addAddress/addAddress.js
const request = require('../../util/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address:0,
        receiver: '张三',
        phone: '17708313281',
        area: 'xxx',
        region: [],
        city:''
    },

    //输入框缓存
    input(e) {
        let data = {}
        data[e.currentTarget.dataset.key] = e.detail.value
        this.setData(data)
    },

    addAddress() {
        let address = {
            memberId: wx.userInfo._id,
            receiver: this.data.receiver,
            phone: this.data.phone,
            city: this.data.region.join(''),
            area: this.data.area,
            addressId: this.data.address
        }
        //验证收货人
        if (!address.receiver || !/^[\u2E80-\u9FFF]+$/.test(address.receiver)) {
            wx.showToast({
                title: '请输入正确的收货人',
            })
            return false
        }
        if (!address.phone || !/^1\d{10}/.test(address.phone)) {
            wx.showToast({
                title: '请输入正确电话',
            })
            return false
        }
        if (!address.city && !this.data.address) {
            wx.showToast({
                title: '请选择地址',
            })
            return false
        }
        if(!this.data.address){///新增
            request.post('http://localhost:3000/wx/address', address).then(res => {
                if (!res.errorMsg) {
                    wx.showToast({
                        title: '新增成功',
                        icon: 'success',
                        duration: 2000
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                          delta: 1,
                        })
                    }, 2000);
                } else {
                    wx.showToast({
                        title: '新增失败',
                        icon: 'success',
                        duration: 2000
                    })
                }
            })
        }else{
            request.put('http://localhost:3000/wx/address/update',address).then(res =>{
                if (!res.errorMsg) {
                    wx.showToast({
                        title: '修改成功',
                        icon: 'success',
                        duration: 2000
                    })
                    setTimeout(() => {
                        wx.navigateBack({
                          delta: 1,
                        })
                    }, 2000);
                } else {
                    wx.showToast({
                        title: '修改失败',
                        icon: 'success',
                        duration: 2000
                    })
                }
            })
        }
    },

    //改变picker
    bindRegionChange: function (e) {
        this.setData({
            region: e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options.address){
            this.setData({
                address:options.address
            })
            request.get('http://localhost:3000/wx/address/addAddress?address='+this.data.address).then(res =>{
                this.setData({
                    city:res[0].city,
                    receiver:res[0].receiver,
                    phone:res[0].phone,
                    area:res[0].area
                })
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})